package com.zhijian.wharf.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.dao.ApiLogMapper;
import com.zhijian.wharf.entity.po.ApiLog;
import com.zhijian.wharf.entity.vo.request.ApiLogSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.request.ApiLogSearchVo;
import com.zhijian.wharf.entity.vo.request.UserSearchVo;
import com.zhijian.wharf.entity.vo.response.ApiLogStaByUserVo;
import com.zhijian.wharf.entity.vo.response.ApiLogStaVo;
import com.zhijian.wharf.entity.vo.response.ApiLogVo;
import com.zhijian.wharf.entity.vo.response.UserVo;
import com.zhijian.wharf.service.ApiLogService;
import com.zhijian.wharf.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * API日志业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-06-17 19:29:00
 */
@Slf4j
@Service
public class ApiLogServiceImpl implements ApiLogService {

    @Autowired
    private ApiLogMapper mapper;

    @Autowired
    private UserService userService;

    @Override
    public ApiLog selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ApiLogVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(ApiLog apiLog) {
        Date now = new Date();
        apiLog.setCreateTime(now);
        apiLog.setUpdateTime(now);
        return mapper.insert(apiLog) > 0 ? apiLog.getId() : 0L;
    }

    @Override
    public Long insertSelective(ApiLog apiLog) {
        Date now = new Date();
        apiLog.setCreateTime(now);
        apiLog.setUpdateTime(now);
        try {
            return mapper.insertSelective(apiLog) > 0 ? apiLog.getId() : 0L;
        } catch (Exception e) {
            //ignore
        }
        return null;
    }

    @Override
    public boolean update(ApiLog apiLog) {
        Date now = new Date();
        apiLog.setUpdateTime(now);
        return mapper.updateByPrimaryKey(apiLog) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(ApiLog record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(ApiLogSaveOrUpdateVo saveOrUpdateVo) {
        ApiLog apiLog = new ApiLog();
        BeanUtils.copyProperties(saveOrUpdateVo, apiLog);
        return this.insert(apiLog) != 0;
    }

    @Override
    public boolean modify(ApiLogSaveOrUpdateVo saveOrUpdateVo) {
        ApiLog old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        ApiLog apiLog = new ApiLog();
        BeanUtils.copyProperties(saveOrUpdateVo, apiLog);
        return this.update(apiLog);
    }

    @Override
    public ApiLogVo selectOne(ApiLogSearchVo apiLogSearchVo) {
        return mapper.selectOne(apiLogSearchVo);
    }

    @Override
    public Integer count(ApiLogSearchVo apiLogSearchVo) {
        return mapper.count(apiLogSearchVo);
    }

    @Override
    public List<ApiLogVo> selectList(ApiLogSearchVo apiLogSearchVo) {
        return mapper.selectList(apiLogSearchVo);
    }

    @Override
    public Page<ApiLogVo> selectPage(ApiLogSearchVo apiLogSearchVo) {
        AssertUtil.check(Objects.isNull(apiLogSearchVo.getPageNum()) || Objects.isNull(apiLogSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(apiLogSearchVo);
        //分页
        Page<ApiLogVo> page = new Page<>(true, count, apiLogSearchVo.getPageNum(), apiLogSearchVo.getPageSize());
        apiLogSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ApiLogVo> responseVos = this.selectList(apiLogSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<ApiLog> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(ApiLogSearchVo apiLogSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(apiLogSearchVo);
    }

    @Override
    public ApiLogStaVo sta() {
        ApiLogSearchVo searchVo = new ApiLogSearchVo();
        searchVo.setMethod("POST");
        Integer postCount = this.count(searchVo);
        searchVo.setMethod("GET");
        Integer getCount = this.count(searchVo);
        searchVo.setMethod("DELETE");
        Integer deleteCount = this.count(searchVo);
        searchVo.setMethod("PUT");
        Integer putCount = this.count(searchVo);
        return ApiLogStaVo.builder()
                .postCount(postCount)
                .getCount(getCount)
                .deleteCount(deleteCount)
                .putCount(putCount)
                .build();
    }

    @Override
    public List<ApiLogStaByUserVo> apiLogStaByUserVo() {
        List<UserVo> userVos = userService.selectList(UserSearchVo.builder().build());
        return userVos.stream().map(userVo -> {
            ApiLogStaByUserVo apiLogStaByUserVo = new ApiLogStaByUserVo();
            apiLogStaByUserVo.setName(userVo.getName());
            ApiLogSearchVo searchVo = new ApiLogSearchVo();
            searchVo.setCreateUserId(userVo.getId());
            Integer count = count(searchVo);
            apiLogStaByUserVo.setCount(Long.valueOf(count + ""));
            return apiLogStaByUserVo;
        }).sorted(Comparator.comparing(ApiLogStaByUserVo::getCount).reversed()).collect(Collectors.toList());
    }

}